#!C:/perl/bin/perl
#
#  Author: mina	
#  Email: mina.ramses@gmail.com
#  Web: http://minaramses.homelinux.org
#  Date: 11.12.2005
#
#  Description: todo.cgi is a very simple perl script that reads and writes to a 
#               text file a 'to do' list, consisting of one item per line. 
#               Nothing fancy here folks, just a little learning tool.
#               If you're a beginner, you might find it useful, if you've
#               written cgi's in perl before, this'll all seem very trivial.
#
#      License: This software is licensed under the GNU General Public License.
#               A copy of the GNU GPL can be found at: http://www.gnu.org/copyleft/gpl.txt
#               This software is open source. This software is free software.



#####################################################
# Read input
# Alternately you could use cgi-lib.pl or CGI.pm for this
#

read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
  ($name, $value) = split(/=/, $pair);
  $value =~ tr/+/ /;
  $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
  $value =~ s/~!/ ~!/g;
  $in{$name} = $value;
}


#####################################################
# Set up variables
# This is where you'll need to determine if you
# want a relative or absolute path for $url
# and where the $file is located.

$todo = $in{'todo'} if defined ($in{'todo'});
$url = "todo.cgi";
$file = "todo.txt";


#####################################################
# These are the three subroutines we call

&deletefromlist;
&addtolist;
&showlist;


# This is really where the script ends
# We just run these three subroutines, and that's it.
# From here on down it's just the subroutines.


#####################################################
# Show list of items subroutine

sub showlist {

open (FILE, "<$file");
@lines = <FILE>;
close (FILE);


#####################################################
# Here we print the top of the html page

print "Content-type: text/html\n\n";
print <<HTMLTOP;
<HTML>
<HEAD>
<TITLE>To Do List</TITLE>
</HEAD>
<BODY BGCOLOR=D0D0FF>
<FORM METHOD="post" ACTION="$url">

<TABLE BGCOLOR=D0D0DD BORDER=1 CELLPADDING=5 CELLSPACING=1>
<TR>
<TD BGCOLOR=000000 ALIGN=LEFT VALIGN=TOP COLSPAN=2><FONT COLOR=CCCCCC><B>To Do List</B></FONT></TD>
</TR>
<TR>
<TD BGCOLOR=999999 ALIGN=RIGHT VALIGN=TOP>Delete Items:</TD>
<TD ALIGN=LEFT VALIGN=TOP>

HTMLTOP


#####################################################
# Here we print items that we pulled in from the text file

$linect = 0;
foreach $line (@lines) {
  chop $line;
  $linect++; 
  print qq{<INPUT NAME="$linect" TYPE="checkbox" VALUE="delete"> $line<BR>\n};
}


#####################################################
# Here we print the bottom of the html page

print <<HTMLBOT;

</TD>
</TR>
<TR>
<TD BGCOLOR=999999 ALIGN=RIGHT VALIGN=TOP>Add Item:</TD>
<TD ALIGN=LEFT VALIGN=TOP>
<INPUT TYPE="text" NAME="todo" VALUE="" SIZE="30">
</TD>
</TR>
<TR>
<TD BGCOLOR=999999 ALIGN=CENTER VALIGN=TOP COLSPAN=2><INPUT TYPE="submit" VALUE=" Update "></FORM></TD>
</TABLE>
</BODY>
</HTML>

HTMLBOT

}


#####################################################
# Add to the list of items subroutine

sub addtolist {
 open (FILE, ">>$file");
 print FILE "$todo\n" unless ($todo eq "");
 close (FILE);
}


#####################################################
# Delete from the list of items subroutine

sub deletefromlist {
 open (FILE, "<$file");
 @lines = <FILE>;
 close (FILE);

 open (FILE, ">$file");
 $linect=0;
 foreach $line (@lines) {
   $linect++;
   chop $line;
   if ($in{$linect} ne "delete") {
     print FILE "$line\n";
   }
 }
 close (FILE);
}


#####################################################
# END




